package lime.browser.view;

import lime.common.LimeException;
import lime.model.RepositoryObject;
import lime.service.RepositoryService;
import lime.service.SpringApplicationContext;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import com.documentum.fc.client.IDfPersistentObject;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.IDfId;

public class AttributesView extends ViewPart {

	public static final String ID = "lime.browser.view.attributes";

	// Widgets
	private Text attributesText;

	// Services
	private RepositoryService repositoryService;

	@Override
	public void init(IViewSite site) throws PartInitException {
		super.init(site);

		repositoryService = (RepositoryService) SpringApplicationContext.getBean("repositoryService");

		getSite().getPage().addSelectionListener(new ISelectionListener() {
			@Override
			public void selectionChanged(IWorkbenchPart part, ISelection selection) {
				if (selection != null && selection instanceof IStructuredSelection) {
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
					Object element = structuredSelection.getFirstElement();
					if (element != null && element instanceof RepositoryObject) {
						RepositoryObject repositoryObject = (RepositoryObject) element;
						update(repositoryObject);
					}
				}
			}
		});
	}

	@Override
	public void createPartControl(Composite parent) {
		attributesText = new Text(parent, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		attributesText.setFont(JFaceResources.getTextFont());
		attributesText.setEditable(false);

		attributesText.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent event) {
				String selectionText = attributesText.getSelectionText();
				if (DfId.isObjectId(selectionText)) {
					RepositoryObject repositoryObject = new RepositoryObject(selectionText);
					update(repositoryObject);
				}
			}
		});
	}

	@Override
	public void setFocus() {
		this.attributesText.setFocus();
	}

	private void update(final RepositoryObject repositoryObject) {

		final Thread myThread = new Thread("MyThread") {
			public void run() {
				try {
					String id = repositoryObject.getObjectId();
					IDfId objectId = new DfId(id);
					IDfSession session = repositoryService.getSession();
					IDfPersistentObject persistentObject = (IDfPersistentObject) session.getObject(objectId);
					final String data = persistentObject.dump();

					getSite().getShell().getDisplay().asyncExec(new Runnable() {
						public void run() {
							attributesText.setText(data);
						}
					});
				} catch (DfException e) {
					throw new LimeException(e);
				}
			}
		};
		myThread.start();

	}
}
